/*
 ----------------------------------------------------------------------------------
                           "THE BEER-WARE COPYRIGHT LICENSE"
                                     (Revision 43)
                                      -----------
    Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
    this file has been sent directly to You by the author, You can do whatever You
    want with this stuff as long as You don't sell or rent it. If we meet some day,
    and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                 __________
                                                                 Paweł Rymaszewski
  ----------------------------------------------------------------------------------
 */

package ui.windows.forms.serverForm;

import exceptions.NoServerException;
import processing.logic.Search;
import processing.objects.Server;
import ui.messages.MessageBoxes;
import ui.windows.forms.searchForm.SearchForm;
import utils.properties.Configuration;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;

/**
 * Action listener for action button from ServerForm
 *
 * @author rymek
 *         User: Rymek
 *         Date: 20.07.11
 *         Time: 20:02
 */
class ServerFormActionListener implements ActionListener {

    private final ServerForm serverForm;        // parent ServerForm

    /**
     * Constructor.
     * Sets reference to parent serverForm
     *
     * @param form parent Search FOrm
     */
    public ServerFormActionListener(ServerForm form) {
        this.serverForm = form;
    }

    /**
     * Performs actions bound ServerForm
     *
     * @param e the action
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        this.connect();
    }

    /**
     * Connects to specific database
     */
    private void connect() {

        try {
            Configuration.production = this.serverForm.getProductionMode().isSelected();

            Server server = this.serverForm.getServerInformationPanel().getServerInfo();
            SearchForm searchForm = new SearchForm(new Search(server), server);
            searchForm.createSearchWindow();
            this.serverForm.minimizeFrame();

            if (this.serverForm.getSaveDetailsCheckBox().isSelected() && this.shouldSaveDetails(server)) {

                Configuration.serverProperties.storeCredentials(server);
                Configuration.serverProperties.exportServers();
            }

        } catch (SQLException error) {
            MessageBoxes.sqlException(error);

        } catch (NoServerException error) {
            MessageBoxes.noServerException(error);

        } catch (IOException error) {
            MessageBoxes.genericErrorMessage(error);
        }

    }

    /**
     * Checks if server details should be saved to servers.config
     *
     * @param server server details to check
     * @return true if save should be done, false otherwise
     */
    private boolean shouldSaveDetails(Server server) {

        String userName = Configuration.serverProperties.getUsername(server);
        String userPass = Configuration.serverProperties.getPassword(server);

        return userName == null && userPass == null || userName == null || userPass == null || !userName.equals(server.getUserName()) || !userPass.equals(server.getUserPass());

    }
}
